package org.glorylight.hacks.social.controller;

import java.util.List;

import javax.ws.rs.QueryParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.glorylight.hacks.social.data.ScrapBookData;
import org.glorylight.hacks.social.model.User;
import org.glorylight.hacks.social.service.FeedService;
import org.glorylight.hacks.social.service.UserService;
import org.glorylight.hacks.social.util.UUIDHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
@Path("/feeds")
public class FeedController extends AbstractBaseController {

	@Autowired
	private FeedService feedService;
	
	@Autowired
	private UserService userService;
	
//	@Deprecated //cannot get feeds without login
//	@GET
//	@Produces(MediaType.APPLICATION_JSON)
//	public List<ScrapBookData> getFeeds() {
//		return getFeedList(getActiveLogin());
//	}
	
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/{userId}")
	public List<ScrapBookData> getFeedsForUser(@PathParam("userId") String userId, @QueryParam("firstResult") int firstResult, 
			@QueryParam("maxResult") int maxResult ) {
		User user = userService.findUserId(UUIDHelper.stringToUUID(userId));
		return getFeedList(user.getEmailAddress(), firstResult, maxResult);
	}
	
	private List<ScrapBookData> getFeedList(String email, int firstResult, int maxResult) {
		return feedService.getFeedByUserPage(email, firstResult, maxResult);
	}
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/latest/{userId}")
	public List<ScrapBookData> getNewestFeeds(@PathParam("userId") String userId) {
		User user = userService.findUserId(UUIDHelper.stringToUUID(userId));
		return getFeedList(user.getEmailAddress(), 0, 3);
	}
	
}
